package com.jrx.anytxn.customer.controller;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.customer.dto.creditinfo.CreditFreezeOfCmsReq;
import com.jrx.anytxn.customer.dto.customer.CustomerLimitAdjustRes;
import com.jrx.anytxn.customer.dto.customer.CustomerLimitInfoRes;
import com.jrx.anytxn.customer.handel.ICustomerLimitInfoHandler;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.constant.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * cms页面相关的客户额度的调额/冻结/解冻
 * @author xuhui.gao
 * @date 2020/8/28 14:50
 */
@Api(tags = "cms页面相关客户管理的api接口")
@RestController
@RequestMapping(value = "/customer/limit")
public class CustomerLimitCmsController {
    private static Logger logger = LoggerFactory.getLogger(CustomerLimitCmsController.class);

    @Autowired
    private ICustomerLimitInfoHandler customerLimitInfoHandler;

    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "cms页面相关客户额度冻结/解冻/调额/注销", notes = "cms页面相关客户额度冻结/解冻/调额/注销")
    @PostMapping(value = "/creditFreezeRelease")
    public TxnRespResult<CustomerLimitAdjustRes> modifyCreditFreeze(@RequestBody CreditFreezeOfCmsReq creditFreezeReq) {
        TxnRespResult<CustomerLimitAdjustRes> result = new TxnRespResult();
        CustomerLimitAdjustRes customerLimitAdjustRes = new CustomerLimitAdjustRes();
        customerLimitAdjustRes.setLimitId(creditFreezeReq.getLimitId());
        //综管获取用户编号
        String userId = String.valueOf(infoService.getUser().getUserName());
        creditFreezeReq.setUserId(userId);
        try {
            List<CustomerLimitInfoRes> cmCustomerLimitInfoList = customerLimitInfoHandler.modifyCreditFreezeByCmsClient(creditFreezeReq);
            customerLimitAdjustRes.setCustomerLimitInfoResList(cmCustomerLimitInfoList);
            result.getSuccess("cms页面相关客户额度冻结/解冻/调额/注销操作成功", customerLimitAdjustRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.setData(customerLimitAdjustRes);
            result.getFail(e.getErrCode(), e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * cms客户额度注销校验
     * @param creditFreezeReq
     * @return
     */
    @ApiOperation(value = "cms客户额度注销校验", notes = "cms客户额度注销校验")
    @PostMapping(value = "/cancelLimitVerify")
    public TxnRespResult<CustomerLimitAdjustRes> cancelLimitVerify(@RequestBody CreditFreezeOfCmsReq creditFreezeReq) {
        TxnRespResult<CustomerLimitAdjustRes> result = new TxnRespResult();
        CustomerLimitAdjustRes customerLimitAdjustRes = new CustomerLimitAdjustRes();
        customerLimitAdjustRes.setLimitId(creditFreezeReq.getLimitId());
        //综管获取用户编号
        String userId = String.valueOf(infoService.getUser().getUserName());
        creditFreezeReq.setUserId(userId);
        try {
            customerLimitAdjustRes = customerLimitInfoHandler.cancelLimitVerify(creditFreezeReq);
            boolean flag = Constant.YES_FLAG.equalsIgnoreCase(customerLimitAdjustRes.getCancelFlag());
            result.getSuccess(flag ? "cms客户额度注销校验成功" : "cms客户额度注销校验失败", customerLimitAdjustRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.setData(customerLimitAdjustRes);
            result.getFail(e.getErrCode(), e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
